<?php

include _ITSTOCK_INCLUDE_DIR_.'html_options.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_form.inc.php';

itstock_structure($structure, 'location');
$edit = false;

if(isset($_POST['id'])){
  $structure['where'][0] = sprintf("`%s`.`id`='%s'", $structure['table'], itstock_decode($_POST['id']));
  $edit = itstock_search_all($structure);
}

echo '<b>'.(!$edit?'New ':'').'Location</b><br><br><br><p class="error" id="e_msg" style="display: none;"></p>';
itstock_html_form_open($structure['type'], ($edit?itstock_encode($structure['data'][0]['id']):''));
?>
    <tr>
      <td><label>Building:</label></td>
      <td><select id="building_id" required autofocus onchange="itstock_request_options('department_id', 'department', 'department', 'building_id');"><?php
        itstock_html_options('building', 'building', 'building_id', $structure, $edit);
      ?></select>
        </td>
    </tr>
    <tr>
      <td><label>Department:</label></td>
      <td><select name="department_id" id="department_id" required ><?php
        itstock_structure($departments, 'department');
        if($edit){
          $departments['where'][0] = 'building_id='.$structure['data'][0]['building_id'];
          itstock_html_options(null, 'department', 'department_id',  $structure, $edit, $departments);
        } else {
          echo '<option value="">Please select a Building</option>';
        }
      ?></select>
        </td>
    </tr>
<?php
itstock_html_form_input_text('Location Name:', 'location' ,($edit?$structure['data'][0]['location']:''), '(Optional)');
itstock_html_form_input_text('Floor #:', 'floor' ,($edit?$structure['data'][0]['floor']:''), 'Floor #', 'required');
itstock_html_form_input_text('Spot:', 'spot' ,($edit?$structure['data'][0]['spot']:''), 'Spot', 'required');
itstock_html_form_close();
?>